package com.atguigu.demo.service.impl;

import com.atguigu.demo.entity.Tiger;
import com.atguigu.demo.mapper.TigerMapper;
import com.atguigu.demo.service.api.TigerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TigerServiceImpl implements TigerService {

    @Autowired
    private TigerMapper tigerMapper;

    @Override
    public List<Tiger> getTigerList() {
        return tigerMapper.selectTigerList();
    }

    @Override
    public void removeTigerById(Integer tigerId) {
        tigerMapper.deleteTigerById(tigerId);
    }

    @Override
    public void saveTiger(Tiger tiger) {
        tigerMapper.insertTiger(tiger);
    }

    @Override
    public Tiger getTigerById(Integer tigerId) {
        return tigerMapper.selectTigerById(tigerId);
    }

    @Override
    public void updateTiger(Tiger tiger) {
        tigerMapper.updateTiger(tiger);
    }

    @Override
    public PageInfo<Tiger> getTigerPage(Integer pageNum, Integer pageSize, String tigerName) {

        // 1、调用 PageHelper 的分页设定方法
        PageHelper.startPage(pageNum, pageSize);

        // 2、调用根据条件执行查询的方法
        // ※特殊操作：为了防止 SQL 执行时把 tigerName 的值当做字段名称，所以需要给 tigerName 两边加上引号
        tigerName = "'%" + tigerName + "%'";

        List<Tiger> tigerList = tigerMapper.selectTigerListByCondition(tigerName);

        // 3、把查询结果封装到 PageInfo 对象中
        return new PageInfo<>(tigerList);
    }
}
